package com.practice.system.controller;

import com.alibaba.fastjson.JSON;
import com.practice.system.mapper.DeviceInfoMapper;
import com.practice.system.pojo.DeviceInfo;
import com.practice.system.service.DeviceInfoService;
import com.practice.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("device_info")
public class DeviceInfoController {

    @Autowired
    DeviceInfoService deviceInfoService;
    @Autowired
    DeviceInfoMapper deviceInfoMapper;

    /**
     * 设备信息管理数据列表显示
     */
    @PostMapping("/list")
    public R list(@RequestBody Map<String, Object> params) {
        return deviceInfoService.queryPage(params);
    }

    /**
     * 修改设备信息
     */
    @PostMapping("/device_update")
    public R updateInfo(@RequestBody DeviceInfo deviceInfo) {
        return deviceInfoService.updateInfo(deviceInfo);
    }

    /**
     * 删除设备
     */
    @GetMapping("/device_delete")
    public R deleteDevice(@RequestParam("id") Integer id) {
        Boolean b = deviceInfoMapper.deleteByidOne(id);
        if (b) {
            return R.ok("删除成功！");
        }
        return R.error("删除失败");
    }

    /**
     * 新建设备信息
     */
    @PostMapping("/device_add")
    public R deviceAdd(@RequestBody DeviceInfo deviceInfo) {
        if (deviceInfo.getDeviceName() == null) {
            return R.error("设备名称必须填写！");
        }
        Integer b = deviceInfoMapper.addDevice(deviceInfo);
        if (b == 1) {
            return R.ok("新增成功！");
        }
        return R.error("新增失败！");
    }

}
